package com.zslibrary.StateHandle;

import android.util.Log;

import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Enum.StatusDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Protocol.Poll;
import com.zslibrary.Protocol.QueryBoardStatus;
import com.zslibrary.Tools.ZSLog;

public class BusyState extends  StateHandle {
    private Pipe mPipe;
    public BusyState(Pipe pipe){
        mPipe=pipe;
    }

    @Override
    public @StateEventDefine.StateEvent int handle() {
        ZSLog.d("busy state");
        @StatusDefine.BoardStatus int boardStatus=QueryBoardStatus.queryBoardStatus(mPipe);
        if(boardStatus==-1){
            return StateEventDefine.EVT_DISCONNECT;
        }
        if(boardStatus==StatusDefine.CMS_IDLE)
            return StateEventDefine.EVT_NONE;
        if(boardStatus==StatusDefine.CMS_AFTERBOOT)
            return StateEventDefine.EVT_RESET;
        if(boardStatus==StatusDefine.CMS_TESTING)
            return StateEventDefine.EVT_EXECUTE;    /*继续busy*/
        if(boardStatus==StatusDefine.CMS_PRODUCT)
            return StateEventDefine.EVT_EXECUTE;    /*继续busy*/
        if(boardStatus==StatusDefine.CMS_DIAGNOSIS)
            return StateEventDefine.EVT_EXECUTE;
        return StateEventDefine.EVT_NONE;
    }

    @Override
    public @StateDefine.State int getNextHop(@StateEventDefine.StateEvent int event) {
        if(event==StateEventDefine.EVT_NONE)
            return StateDefine.STATE_IDLE;
        if(event==StateEventDefine.EVT_EXECUTE)
            return StateDefine.STATE_BUSY;
        if(event==StateEventDefine.EVT_RESET)
            return StateDefine.STATE_INIT;
        if(event==StateEventDefine.EVT_RECONNECT)
            return StateDefine.STATE_RECONNECT;
        return StateDefine.STATE_IDLE;
    }
}
